package com.live.adapter;

import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.live.activity.LiveFriendHomeActivity;
import com.live.activity.R;
import com.live.common.Constant;
import com.live.common.ImageDownloader;

public class LiveInfoMsgSA extends SimpleAdapter {

	public List<Map<String, Object>> list;
	public Context context;
	public String uId;
	public int index = 0;
	public List<Map<String, Object>> items;

	private final ImageDownloader imageDownloader = new ImageDownloader();

	public LiveInfoMsgSA(Context context, List<Map<String, Object>> items, int resource, String[] from, int[] to,
			String uId) {
		super(context, items, resource, from, to);
		this.list = items;
		this.context = context;
		this.uId = uId;
		this.items = items;
	}

	@Override
	@SuppressWarnings("unchecked")
	public View getView(final int position, View convertView, ViewGroup parent) {

		if (convertView == null) {
			LayoutInflater inflater = ((Activity) context).getLayoutInflater();
			convertView = inflater.inflate(R.layout.info_msg_list, null, true);
		}
		if (position <= list.size()) {
			final Map<String, Object> obj = (Map<String, Object>) getItem(position);

			// 设置内容
			TextView content = (TextView) convertView.findViewById(R.id.content);
			content.setText(Html.fromHtml("<font color='#2D86B0'>" + obj.get("userId") + "</font>" + " 留言 "
					+ "<font color='#2D86B0'>" + obj.get("toUserId") + "</font> " + obj.get("content")));

			// 设置城市
			TextView userCity = (TextView) convertView.findViewById(R.id.userCity);
			if (!"null".equals(obj.get("userCity").toString())) {
				userCity.setText((String) obj.get("userCity"));
			}

			// 设置性别
			TextView userSex = (TextView) convertView.findViewById(R.id.userSex);
			if (Constant.USER_SEX_MAN.equals((String) obj.get("userSex"))) {
				userSex.setText("♂");
				userSex.setTextColor(0xff00CCFF);
			} else {
				userSex.setText("♀");
				userSex.setTextColor(0xffFF6699);
			}

			// 设置年龄
			TextView userAge = (TextView) convertView.findViewById(R.id.userAge);
			userAge.setText((String) obj.get("userAge"));

			// 设置日期
			TextView updateDate = (TextView) convertView.findViewById(R.id.updateDate);
			updateDate.setText((String) obj.get("updateDate"));

			// 设置头像
			ImageView userPic = (ImageView) convertView.findViewById(R.id.userPic);
			if (Constant.Y.equals(obj.get("userPic"))) {
				String imageUrl = Constant.SITE_URL + Constant.SITE_AVATAR_PATH + obj.get("uId") + ".jpg";
				imageDownloader.download(imageUrl, userPic, (String)obj.get("userPicVer"));
			} else {
				userPic.setImageResource(R.drawable.default_pic);
			}
			userPic.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					Intent intent = new Intent();
					intent.setClass(context, LiveFriendHomeActivity.class);
					Bundle bundle = new Bundle();
					bundle.putString("f", (String) obj.get("f"));
					bundle.putString("uId", (String) obj.get("uId"));
					bundle.putString("userId", (String) obj.get("userId"));
					bundle.putString("userSign", (String) obj.get("userSign"));
					bundle.putString("userSex", (String) obj.get("userSex"));
					bundle.putString("userAge", (String) obj.get("userAge"));
					if (!"null".equals(obj.get("userCity").toString())) {
						bundle.putString("userCity", (String) obj.get("userCity"));
					} else {
						bundle.putString("userCity", context.getResources().getString(R.string.user_city_no));
					}
					bundle.putString("userPic", (String) obj.get("userPic"));
					bundle.putString("userPicVer", (String) obj.get("userPicVer"));
					intent.putExtras(bundle);
					context.startActivity(intent);
				}
			});
		}
		return convertView;
	}

	public ImageDownloader getImageDownloader() {
		return imageDownloader;
	}
}